const router = require('koa-router')()
router.prefix('/api/user')
const { HttpException } = require('@core/http-exception')
const { User } = require('@model/user.js')
const { RegisterValidator } = require('@validator/validator.js')
const { SuccessModel } = require('@model/resModel')
const { Auth } = require('@middlewares/auth.js')

router.post('/register', new Auth(7).controllerAuthApi, async (ctx, next) => {
  const v = await new RegisterValidator().validate(ctx)
  const email = v.get('body.email')
  const password = v.get('body.password2')
  await User.create({
    email,
    password
  })
  const request = `${ctx.method} ${ctx.path}`
  ctx.body = new SuccessModel({
    email,
    password
  }, request)
})

router.get('/bar', function (ctx, next) {
  ctx.body = 'this is a users/bar response'
})

module.exports = router